/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.qx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.formula.functions.Count;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sun.mail.util.ReadableMime;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.qx.entity.QxLeagueMatches;
import com.thinkgem.jeesite.modules.qx.entity.QxTeam;
import com.thinkgem.jeesite.modules.qx.service.QxLeagueMatchesService;
import com.thinkgem.jeesite.modules.qx.service.QxTeamService;
import com.thinkgem.jeesite.modules.qx.utils.QxCacheUtils;

/**
 * 单表生成Controller
 * @author zequs
 * @version 2017-08-22
 */
@Controller
@RequestMapping(value = "${adminPath}/qx/qxLeagueMatches")
public class QxLeagueMatchesController extends BaseController {

	@Autowired
	private QxLeagueMatchesService qxLeagueMatchesService;
	
	@Autowired
	private QxTeamService qxTeamService;
	
	@ModelAttribute
	public QxLeagueMatches get(@RequestParam(required=false) String id) {
		QxLeagueMatches entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = qxLeagueMatchesService.get(id);
		}
		if (entity == null){
			entity = new QxLeagueMatches();
		}
		return entity;
	}
	
	@RequiresPermissions("qx:qxLeagueMatches:view")
	@RequestMapping(value = {"list", ""})
	public String list(QxLeagueMatches qxLeagueMatches, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<QxLeagueMatches> page = qxLeagueMatchesService.findPage(new Page<QxLeagueMatches>(request, response), qxLeagueMatches); 
		model.addAttribute("page", page);
		return "modules/qx/qxLeagueMatchesList";
	}

	@RequiresPermissions("qx:qxLeagueMatches:view")
	@RequestMapping(value = "form")
	public String form(QxLeagueMatches qxLeagueMatches, Model model) {
		model.addAttribute("qxLeagueMatches", qxLeagueMatches);
		return "modules/qx/qxLeagueMatchesForm";
	}

	@RequiresPermissions("qx:qxLeagueMatches:edit")
	@RequestMapping(value = "save")
	public String save(QxLeagueMatches qxLeagueMatches, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, qxLeagueMatches)){
			return form(qxLeagueMatches, model);
		}
		qxLeagueMatchesService.save(qxLeagueMatches);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/qx/qxLeagueMatches/?repage";
	}
	
	@RequiresPermissions("qx:qxLeagueMatches:edit")
	@RequestMapping(value = "delete")
	public String delete(QxLeagueMatches qxLeagueMatches, RedirectAttributes redirectAttributes) {
		qxLeagueMatchesService.delete(qxLeagueMatches);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/qx/qxLeagueMatches/?repage";
	}
	@RequiresPermissions("qx:qxLeagueMatches:strength")
	@RequestMapping(value = "strength")
	public String strength(QxTeam qxTeam, RedirectAttributes redirectAttributes) {
//	    List<QxTeam> teams = qxTeamService.
	    int count = qxTeamService.count(qxTeam);
	    int strongCount = qxTeam.getLimitStart();
	    int weakCount = qxTeam.getLimitEnd();
	    if (count < strongCount + weakCount || strongCount <= 0 || weakCount <= 0) {
	    	addMessage(redirectAttributes, "强弱队数量设置错误");
	    } else {
	    	qxTeam.setLimitStart(0);
	    	qxTeam.setLimitEnd(strongCount);
	    	List<QxTeam> strongTeams = qxTeamService.findStrongTeam(qxTeam);
	    	for (QxTeam team : strongTeams) {
	    		team.setStrength(1);
	    		qxTeamService.save(team);
	    		QxCacheUtils.putCache("team_key"+qxTeam.getId(), qxTeam);
				QxCacheUtils.putCache("team_name_key"+qxTeam.getName(), qxTeam);
			}
	    	qxTeam.setLimitStart(count-weakCount);
	    	qxTeam.setLimitEnd(weakCount);
	    	List<QxTeam> weakTeams = qxTeamService.findStrongTeam(qxTeam);
	    	for (QxTeam team : weakTeams) {
	    		team.setStrength(2);
	    		qxTeamService.save(team);
	    		QxCacheUtils.putCache("team_key"+qxTeam.getId(), qxTeam);
				QxCacheUtils.putCache("team_name_key"+qxTeam.getName(), qxTeam);
			}
	    	qxTeam.setLimitStart(strongCount);
	    	qxTeam.setLimitEnd(count-weakCount-strongCount);
	    	List<QxTeam> otherTeams = qxTeamService.findStrongTeam(qxTeam);
	    	for (QxTeam team : otherTeams) {
	    		team.setStrength(3);
	    		qxTeamService.save(team);
	    		QxCacheUtils.putCache("team_key"+qxTeam.getId(), qxTeam);
				QxCacheUtils.putCache("team_name_key"+qxTeam.getName(), qxTeam);
			}
	    	addMessage(redirectAttributes, "设置成功");
		}
	    return "redirect:"+Global.getAdminPath()+"/qx/qxLeagueMatches/?repage";
	}

}